
package com.moogene.saad.sns.provider;

import com.moogene.saad.sns.Friend;

import java.util.ArrayList;
import java.util.List;

import com.moogene.android.R;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.provider.ContactsContract;
import android.util.Log;

public class ContactSocialGraphProvider {

    public static List<Friend> getFriends(Context context) {
        List<Friend> friends = new ArrayList<Friend>();

        ContentResolver cr = context.getContentResolver();
        Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

        Log.v("saad", "getFriends =>cursor: " + cursor.getCount());

        if (cursor.getCount() > 0) {
            while (cursor.moveToNext()) {

                String phoneNumber = "";
                String sex = "unknow";
                String email = "";

                String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));

                String displayName = cursor.getString(cursor
                        .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

                if(displayName == null || displayName.isEmpty() || displayName.contains("@")){
                    continue;
                }

                if (Integer.parseInt(cursor.getString(cursor
                        .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                    // Query phone here. Covered next
                    if (Integer.parseInt(cursor.getString(cursor
                            .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                        Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
                                new String[] {
                                    id
                                }, null);
                        if (pCur.moveToNext()) {

                            phoneNumber = pCur.getString(pCur
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        }
                        pCur.close();
                    }else{
                        continue;
                    }

                    // if the contacts do not have phone number, it is an
                    // invalid contact.
                    Cursor emailCur = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                            null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
                            new String[] {
                                id
                            }, null);
                    if (emailCur.moveToNext()) {
                        // This would allow you get several email addresses
                        // if the email addresses were stored in an array
                        email = emailCur.getString(emailCur
                                .getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                        String emailType = emailCur.getString(emailCur
                                .getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
                    }
                    emailCur.close();
                }
                Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.android);
                friends.add(new Friend(displayName, sex, phoneNumber, email, bitmap));
            }
        }
        cursor.close();
        return friends;
    }

}
